

import random

from dexen.system.base import score
from dexen.system.base.individual import Individual

import constants

class MyIndividual(Individual):
    def __init__(self):
        Individual.__init__(self)
        self.genotype = None
        
        self.phen_daylight = None
        self.phen_energy = None
        
        self.score_daylight = None
        self.set_score("score_daylight", "Day light", score.MAXIMIZE)

        self.score_energy = None
        self.set_score("score_energy", "Energy Consumption", score.MINIMIZE)
        
        self.score_cost = None
        self.set_score("score_cost", "Cost Factor", score.MINIMIZE)
        
        self.fitness = None
    
    def reproduce(self, ind):
        child1 = MyIndividual()
        child2 = MyIndividual()
        child1.genotype = []
        child2.genotype = []
        
        z = random.randint(1, constants.GEN_SIZE-2)
        
        for i in range(z):
            child1.genotype.append(self.genotype[i])
            child2.genotype.append(ind.genotype[i])
            
        for i in range(z, constants.GEN_SIZE):
            child1.genotype.append(ind.genotype[i])
            child2.genotype.append(self.genotype[i])
        
        return child1, child2
    
    def _rr(self, atr, msg):
        if atr != None:
            return msg
        else: return "!"+msg
    
    def __repr__(self):
        res = "Individual Info: "
        if hasattr(self, "__id__"):
            res += str(self.__id__) 
        res += "\n"
        res += self._rr(self.genotype, "gen") + "\n"
        res += self._rr(self.phen_daylight, "phen") + "\n"
        res += "score_daylight = %s"%self.score_daylight + "\n"
        res += "score_energy = %s"%self.score_energy + "\n"
        res += "score_cost = %s"%self.score_cost + "\n"
        res += "fitness = %s"%self.fitness + "\n"
        return res
